//
//  AddPickerViewController.m
//  Flava
//
//  Created by Ug-Code on 13-8-21.
//  Copyright (c) 2013年 UgCode. All rights reserved.
//

#import "AddDatePickerViewController.h"

@implementation AddDatePickerViewController

@synthesize
titleText = _titleText,
bodyText = _bodyText,
shadowView = _shadowView,
date = _date,
dayButton = _dayButton,
timeButton = _timeButton,
dayPicker = _dayPicker,
timePicker = _timePicker;

- (id)init
{
    self = [super init];
    if (self) {
		//
		_date = [NSDate date];
		//--DayPicker
		_dayPicker = [[UIDatePicker alloc] init];
		_dayPicker.datePickerMode = UIDatePickerModeDate;
		_dayPicker.frame = CGRectMake(0, [UGDevice windowHeight], 320, 216);
		_dayPicker.maximumDate = [NSDate date];
		[_dayPicker addTarget:self action:@selector(upDate:) forControlEvents:UIControlEventValueChanged];
		//--TimePicker
		_timePicker = [[UIDatePicker alloc] init];
		_timePicker.datePickerMode = UIDatePickerModeTime;
		_timePicker.frame = CGRectMake(0, [UGDevice windowHeight], 320, 216);
		_timePicker.maximumDate = [NSDate date];
		[_timePicker addTarget:self action:@selector(upDate:) forControlEvents:UIControlEventValueChanged];
		//
		_dayFormatter = [[NSDateFormatter alloc] init];
		_timeFormatter = [[NSDateFormatter alloc] init];
		[_dayFormatter setDateFormat:@"yyyy-MM-dd"];
		[_timeFormatter setDateFormat:@"a HH:mm"];
		//--BUtton
        _dayButton = [[UGButton alloc] init];
		_timeButton = [[UGButton alloc] init];
		_dayButton.frame = CGRectMake(5, 5, 111, 30);
		_timeButton.frame = CGRectMake(121, 5, 102, 30);
		[_dayButton setNormalImage:[[UIImage imageNamed:@"add_date_bg_n.png"]
									stretchableImageWithLeftCapWidth:30 topCapHeight:0]];
		[_dayButton setHighlightedImage:[[UIImage imageNamed:@"add_date_bg_h.png"]
									stretchableImageWithLeftCapWidth:30 topCapHeight:0]];
		[_timeButton setNormalImage:[[UIImage imageNamed:@"add_date_bg_n.png"]
									stretchableImageWithLeftCapWidth:30 topCapHeight:0]];
		[_timeButton setHighlightedImage:[[UIImage imageNamed:@"add_date_bg_h.png"]
										  stretchableImageWithLeftCapWidth:30 topCapHeight:0]];
		_dayButton.customTitleLable.text = [_dayFormatter stringFromDate:_date];
		_timeButton.customTitleLable.text = [_timeFormatter stringFromDate:_date];
		_dayButton.customTitleLable.frame = CGRectMake(8, 10, 65, 12);
		_timeButton.customTitleLable.frame = CGRectMake(8, 10, 65, 12);
		_dayButton.customTitleLable.textColor = [UIColor colorWithRed:90.0f/255 green:90.0f/255 blue:90.0f/255 alpha:1];
		_timeButton.customTitleLable.textColor = [UIColor colorWithRed:90.0f/255 green:90.0f/255 blue:90.0f/255 alpha:1];
		_dayButton.customTitleLable.font = [UIFont boldSystemFontOfSize:12];
		_timeButton.customTitleLable.font = [UIFont boldSystemFontOfSize:12];
		_dayButton.customTitleLable.textAlignment = NSTextAlignmentLeft;
		_timeButton.customTitleLable.textAlignment = NSTextAlignmentLeft;
		//
		[_dayButton addTarget:self action:@selector(dateButtonClick:) forControlEvents:UIControlEventTouchUpInside];
		[_timeButton addTarget:self action:@selector(dateButtonClick:) forControlEvents:UIControlEventTouchUpInside];
		//
		_shadowView = [[UGView alloc] init];
		if ([UGDevice systemVersion] >= 7) {
			_shadowView.frame = CGRectMake(0, 0, 320, 1000);
		} else {
			_shadowView.frame = CGRectMake(0, 0, 320, 1000);
		}
		_shadowView.backgroundColor = [UIColor blackColor];
		_shadowView.alpha = 0;
		_shadowView.hidden = YES;
		UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] init];
		[gesture addTarget:self action:@selector(hiddenPickerView)];
		[_shadowView addGestureRecognizer:gesture];
    }
    return self;
}

/** `_dayButton`和`_timeButton`的触发事件 */
- (void)dateButtonClick:(UGButton *)button
{
	[_titleText resignFirstResponder];
	[_bodyText resignFirstResponder];
	if (button == _dayButton) {
		[UIView animateWithDuration:0.3 animations:^{
			_dayPicker.frame = CGRectMake(0, [UGDevice windowHeight]-64-216, 320, 216);
			_dayPicker.date = _date;
			_shadowView.hidden = NO;
			_shadowView.alpha = 0.3;
			if ([UGDevice systemVersion] >= 7) {
				_shadowView.frame = CGRectMake(0, 0, 320, [UGDevice windowHeight]-216);
			} else {
				_shadowView.frame = CGRectMake(0, 0, 320, [UGDevice windowHeight]-216-20);
			}
		}];
	} else if (button == _timeButton) {
		[UIView animateWithDuration:0.3 animations:^{
			_timePicker.frame = CGRectMake(0, [UGDevice windowHeight]-64-216, 320, 216);
			_timePicker.date = _date;
			_shadowView.hidden = NO;
			_shadowView.alpha = 0.3;
			if ([UGDevice systemVersion] >= 7) {
				_shadowView.frame = CGRectMake(0, 0, 320, [UGDevice windowHeight]-216);
			} else {
				_shadowView.frame = CGRectMake(0, 0, 320, [UGDevice windowHeight]-216-20);
			}
		}];
	} else {
		LOGLINE;
	}
}

/** 更新时间 */
- (void)upDate:(UIDatePicker *)picker
{
	self.date = picker.date;
	[[NSNotificationCenter defaultCenter] postNotificationName:@"ContentDateUpdate" object:_date];
	NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
	[formatter setDateFormat:@"yyyy/MM/dd HH:mm"];
	NSString* dateString = [formatter stringFromDate:picker.date];
	NSLog(@"%@", dateString);
}


/** 隐藏PickerView同时刷新button上的时间 */
- (void)hiddenPickerView
{
	[UIView animateWithDuration:0.3 animations:^{
		_dayPicker.frame = CGRectMake(0, [UGDevice windowHeight], 320, 216);
		_timePicker.frame = CGRectMake(0, [UGDevice windowHeight], 320, 216);
		if ([UGDevice systemVersion] >= 7) {
			_shadowView.frame = CGRectMake(0, 0, 320, 1000);
		} else {
			_shadowView.frame = CGRectMake(0, 0, 320, 1000);
		}
		_shadowView.backgroundColor = [UIColor blackColor];
		_shadowView.alpha = 0;
		_shadowView.hidden = YES;
		[_bodyText becomeFirstResponder];
	}];
	_dayButton.customTitleLable.text = [_dayFormatter stringFromDate:_date];
	_timeButton.customTitleLable.text = [_timeFormatter stringFromDate:_date];
}

@end
